﻿using System;
using System.Threading;

using OpenQA.Selenium;

using NUnit.Framework;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class L001_SimpleSearch {
		private IWebDriver driver;
		private String forumName;
		private const Int32 indexDelay = 15;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();

			forumName = Guid.NewGuid().ToString();

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.CreateForum("Test board", "Category 1", forumName, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName);
			driver.DisableAntiSpam();
			driver.EnableLucene(false, indexDelay);
			driver.LogOff();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		/// <summary>
		/// New topic created, we need to be able to find it, given the forum, both by getting a hit in the title and the body!
		/// Search should be done as the anonymous user!?
		/// </summary>
		[Test]
		public void OneWordOneTopic() {
			String title = "This is supposed to be a meaningful title, the word is lucenesearchtitle";
			String content = "And this is supposed to be a really meaningful body of text. We need to put in some proper text, and hope that search will index it! The word is lucenesearchbody.";

			// TODO: LogOn as...?!?!
			driver.LogOn(Constants.Users.Regular.Email, Constants.Users.Regular.Password);

			driver.CreateTopic(forumName, title, content);

			driver.LogOff();

			// Let the indexer index the new topics!
			driver.Wait(indexDelay * 2);

			driver.Search("lucenesearchtitle", forumName);

			// Let's see the result!!
			Assert.AreEqual(1, driver.FindElements(By.ClassName("topictitle")).Count, "The search word was not found anywhere, should have given us a hit on the title!");

			driver.Search("lucenesearchbody", forumName);

			// Let's see the result!!
			Assert.AreEqual(1, driver.FindElements(By.ClassName("topictitle")).Count, "The search word was not found anywhere, should have given us a hit on the body!");
		}
	}
}